﻿using System.Text;
using System.Security.Cryptography;
using System;

namespace VK.Comm
{
    public static class MSProvider
    {
        private static readonly byte[] _key;
        private static readonly byte[] _initVector;

        private static DESCryptoServiceProvider _provider;

        static MSProvider()
        {
            _provider = new DESCryptoServiceProvider();
            _key = Encoding.Default.GetBytes("-@#__#@-");
            _initVector = Encoding.Default.GetBytes("^-^-+-^-");
        }

        public static string InternalEncrypt(byte[] bArray)
        {
            ICryptoTransform transform = _provider.CreateEncryptor(_key, _initVector);

            byte[] bOutput = transform.TransformFinalBlock(bArray, 0, bArray.Length);

            return Convert.ToBase64String(bOutput);
        }

        public static byte[] InternalDecrypt(string contents)
        {
            ICryptoTransform transform = _provider.CreateDecryptor(_key, _initVector);

            // byte[] bArray = Encoding.Default.GetBytes( Convert.FromBase64String( contents) );
            byte[] bArray = Convert.FromBase64String(contents);
            byte[] bOutput = transform.TransformFinalBlock(bArray, 0, bArray.Length);
            return bOutput;
        }


        public static string Encrypt(string contents)
        {
            byte[] bArray = Encoding.Default.GetBytes(contents);
            return InternalEncrypt(bArray);
        }

        public static string Decrypt(string contents)
        {
            // byte[] bArray = Encoding.Default.GetBytes( Convert.FromBase64String( contents) );
            byte[] bOutput = InternalDecrypt(contents);
            return Encoding.Default.GetString(bOutput);
        }

    }
}
